package com.huangfuyi.datamodel.service.impl;

import com.huangfuyi.common.utils.ObjectUtils;
import com.huangfuyi.datamodel.dao.EDataModelDao;
import com.huangfuyi.datamodel.entity.datadefine.EDataFieldEntity;
import com.huangfuyi.datamodel.entity.datadefine.EDataModelConfExample;
import com.huangfuyi.datamodel.entity.datadefine.EDataModelEntity;
import com.huangfuyi.datamodel.service.EDataModelService;
import com.huangfuyi.datasource.annotate.DataDefineInfo;
import com.huangfuyi.datasource.entity.EDataDefineConf;
import com.huangfuyi.datasource.enums.EDataSourceType;
import com.huangfuyi.datasource.factory.EDataSourceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class EDataModelServiceImpl implements EDataModelService {

    private final Logger log = LoggerFactory.getLogger(EDataModelServiceImpl.class);

    private final String TAG = "数据模型 > 模型管理业务 ==> ";

    private final Map<EDataSourceType, List<EDataModelConfExample>> DATA_MODEL_EXAMPLE_CACHE = new HashMap<>();

    @Autowired
    private EDataModelDao dataModelDao;

    @Override
    public List<EDataModelConfExample> queryDataModelExample(EDataSourceType type) {
        if(DATA_MODEL_EXAMPLE_CACHE.containsKey(type)){
            return DATA_MODEL_EXAMPLE_CACHE.get(type);
        }
        return initDataModelExample(type);
    }

    private synchronized List<EDataModelConfExample> initDataModelExample(EDataSourceType type){
        if(DATA_MODEL_EXAMPLE_CACHE.containsKey(type)){
            return DATA_MODEL_EXAMPLE_CACHE.get(type);
        }
        Class<? extends EDataDefineConf> dataModelExampleClazz = EDataSourceFactory.getDataModelExample(type);
        ArrayList<EDataModelConfExample> result = new ArrayList<>();
        Field[] fields = dataModelExampleClazz.getFields();
        for (Field field : fields) {
            EDataModelConfExample dataModelConfExample = new EDataModelConfExample();
            DataDefineInfo annotation = field.getAnnotation(DataDefineInfo.class);
            if(!ObjectUtils.isEmpty(annotation)){
                dataModelConfExample.setLabel(annotation.label());
                dataModelConfExample.setDescription(annotation.description());
            }
            dataModelConfExample.setName(field.getName());
            result.add(dataModelConfExample);
        }
        DATA_MODEL_EXAMPLE_CACHE.put(type, result);
        return result;
    }

    @Override
    public int createDataModel(EDataModelEntity dataModelInfo) {
        if(){

        }

        return 0;
    }

    @Override
    public int deleteDataModel(EDataModelEntity dataModelInfo) {
        return 0;
    }

    @Override
    public int updateDataModel(EDataModelEntity dataModelInfo) {
        return 0;
    }

    @Override
    public EDataModelEntity queryDataModelByDataSourceId(String dataSourceId) {
        return null;
    }

    @Override
    public int addModelFields(String dataModelId, List<EDataFieldEntity> fields) {
        return 0;
    }

    @Override
    public int updateModelField(EDataFieldEntity field) {
        return 0;
    }

    @Override
    public List<EDataFieldEntity> queryDataFieldList(String modelId, int pageNum, int pageSize) {
        return null;
    }

    @Override
    public List<EDataFieldEntity> queryDataFieldAll(String modelId) {
        return null;
    }
}
